package com.project;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/PaymentServlet")
public class PaymentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user != null) {
            String oid = request.getParameter("oid");
            String payamount = request.getParameter("payamount");
            String aid = request.getParameter("aid");

            List<String> input = new ArrayList<>();
            input.add(oid);
            input.add(aid);
            input.add(payamount);
            input.add("1"); // 支付状态更新为已支付

            if (user.Pay(input)) {
                response.sendRedirect("payment.jsp?payamount=" + payamount);
            } else {
                // 处理支付失败的情况
                response.getWriter().println("支付失败，请重试");
            }
        } else {
            response.sendRedirect("login.jsp");
        }
    }
}